import PopconfirmButton from '@/components/BizButtons/PopconfirmButton';
import useActionControl from '@/hooks/useActionControl';
import ApiSystemDictType from '@/services/system/ApiSystemDictType';
import { DeleteOutlined, EditOutlined } from '@ant-design/icons';
import { ProTable } from '@ant-design/pro-components';
import { Button, Tag } from 'antd';
import ButtonGroup from 'antd/es/button/button-group';

const SystemDictTypeIndex = () => {
  const actionControl = useActionControl({
    addAction: {
      onActionCall: (values) => ApiSystemDictType.add(values),
    },
    editAction: {
      onModalOpen: (values) => ApiSystemDictType.detail(values.dictTypeId),
      onActionCall: (values) => ApiSystemDictType.edit(values),
    },
  });

  actionControl.queryParams.refill();

  return (
    <div>
      <ProTable
        {...actionControl.table}
        request={async (params) => {
          const response = await ApiSystemDictType.list(params);
          return {
            data: response.rows,
            success: true,
            total: response.total,
          };
        }}
        rowKey={'dictType'}
        columns={[
          { title: '字典名称', dataIndex: 'dictName' },
          { title: '字典类型', dataIndex: 'dictType' },
          {
            title: '状态',
            dataIndex: 'status',
            valueEnum: {
              '0': <Tag color={'green'}>正常</Tag>,
              '1': <Tag color={'red'}>停用</Tag>,
            },
          },
          {
            title: '备注',
            dataIndex: 'remark',
            ellipsis: true,
            hideInSearch: true,
          },
          { title: '创建时间', dataIndex: 'createTime', hideInSearch: true },
          {
            title: '操作',
            hideInSearch: true,
            render: (text, record) => [
              <ButtonGroup key={'operations'}>
                <Button
                  key="edit"
                  icon={<EditOutlined />}
                  type={'link'}
                  onClick={() => {
                    actionControl.actions.openUpdateModal(record.dictTypeId);
                  }}
                >
                  修改
                </Button>
                <PopconfirmButton
                  key={'delete'}
                  buttonText={'删除'}
                  title={'删除后不可恢复, 确认删除吗?'}
                  buttonProps={{
                    danger: true,
                    type: 'link',
                    icon: <DeleteOutlined />,
                  }}
                  onConfirm={async () => {
                    await ApiSystemDictType.remove(record.dictTypeId);
                  }}
                />
              </ButtonGroup>,
            ],
          },
        ]}
      />
    </div>
  );
};

export default SystemDictTypeIndex;
